//
//  VisitorView.swift
//  sinaDemo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

@objc protocol VisitorViewDelegate: NSObjectProtocol {
    optional func clickLogin()
    
    optional func clickRegister()
}

class VisitorView: UIView {
    
    // 代理属性
    weak var delegate: VisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置UI界面
        setUpUI()
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVisitor(tipTitle: String, imageName: String?) {
        tipLabel.text = tipTitle
        if let OimageName = imageName { // 不是首页
            circleView.image = UIImage(named: OimageName)
            bringSubviewToFront(circleView)
            // 隐藏大房子
            homeView.hidden = true
        }else { // 是首页
            // 开始动画
            startAnimation()
        }
//        homeView.image = UIImage(named: imageName)
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画属性
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        anim.toValue = 2 * M_PI
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    private func setUpUI() {
        addSubview(circleView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 设置约束
        // 遍历子控件,设置关闭自动frame布局
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 圆
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        // 房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 文字
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 40))
        
        // 注册按钮和登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        // 遮罩
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverView]-0-|", options: [], metrics: nil, views: ["coverView" : coverView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[coverView]-(-35)-[tipLabel]", options: [], metrics: nil, views: ["coverView" : coverView, "tipLabel": tipLabel]))

        
    }
    
    // 懒加载
    // 圆
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 半透明视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 文字
    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.enabled = false
        return button
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "clickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        return button
    }()

    // 按钮的点击事件
    @objc private func clickRegisterButton() {
        // 通知代理点击了注册按钮
        delegate?.clickRegister?()
    }
    
    @objc private func clickLoginButton() {
        // 通知代理点击了登录按钮
        delegate?.clickLogin?()
    }
    
}
